#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sqlite3.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include "../include/staff_db.h" 
#include "../include/server.h"

//MSG_C from_client_msg = {};
//MSG_S to_client_msg = {};

int server_init(void)
{
	int listenfd;
	struct sockaddr_in server_addr;
	memset(&server_addr,0,sizeof(server_addr));
	char logmes[15]; //用于存储iP
	printf("input ip:\n");
	scanf("%s",logmes);
	//创建服务器
	
	//创建套接字
	listenfd = socket(PF_INET,SOCK_STREAM,0);
	if(listenfd < 0)
    {
		perror("fail to socket");
		exit(-1);
	}

	bzero(&server_addr,sizeof(server_addr));
	server_addr.sin_family = PF_INET;
	server_addr.sin_port = htons(8888);
	server_addr.sin_addr.s_addr = inet_addr(logmes);
	//绑定
	if(bind(listenfd,(struct sockaddr *)&server_addr,sizeof(server_addr))<0)
	{
		perror("fail to bind");
		exit(-1);
	}
	//监听
	if(listen(listenfd,5)<0)
	{
		perror("fail to listen");
        return -1;
	}
    return listenfd;
}



//登录函数
//参数：姓名 密码 客户端套接字
//杨宁
int user_login_module(int sockfd,MSG_C *from_client_msg,MSG_S *to_client_msg)
{
	printf("in user_login_module\n");
	if(do_staff_check_info(from_client_msg->text.name,from_client_msg->text.password)==0)
	{
		strcpy(to_client_msg->error,"name or passwd error");
		to_client_msg->type = 'f';
		send(sockfd,to_client_msg,sizeof(MSG_S),0);
		return -1;
	}else{

		to_client_msg->type = 's';
		from_client_msg->text.flag=do_staff_check_identify(from_client_msg->text.name);//0表示用户1表示管理员
		to_client_msg->text.flag = from_client_msg->text.flag;
		send(sockfd,to_client_msg,sizeof(MSG_S),0);
		return 0;
	}	
}
//修改密码
//参数：姓名；密码；客户端套接字
//杨宁
int orderuser_change_passwd_module(char *name,char *passwd,int sockfd,MSG_S to_client_msg)
{

	if(do_staff_change_passwd(name,passwd))
	{
		to_client_msg.type = 'f';
		strcpy(to_client_msg.error,"passwd change fail");
		 send(sockfd,&to_client_msg,sizeof(MSG_S),0);
		 return -1;   //失败返回   
	 }
	 else
	 {
		 to_client_msg.type = 's';
		 strcpy(to_client_msg.error,"passwd change success");
		 send(sockfd,&to_client_msg,sizeof(MSG_S),0);
		 return 0;
	 }	
}
//管理员改变用户信息
//参数:客户端套接字
//杨宁

int admin_change_userinfo_module(int sockfd,MSG_C from_client_msg,MSG_S to_client_msg)
{
/*	if(to_client_msg.text.flag==0)    
	{
		to_client_msg.type = 'f';
		strcpy(to_client_msg.error,"power less");	
		send(sockfd,&to_client_msg,sizeof(MSG_S),0);
		return -1;
	}*/
	if(is_name_exist(from_client_msg.text.name)==0)
	{
		to_client_msg.type = 'f';
		strcpy(to_client_msg.error,"name not exit");
		send(sockfd,&to_client_msg,sizeof(MSG_S),0);
		return -1;  
	}
	if(do_staff_change_userinfo(from_client_msg.text)!=0)
	{
		to_client_msg.type = 'f';
		strcpy(to_client_msg.error,"userinfo change fail");
		send(sockfd,&to_client_msg,sizeof(MSG_S),0);
		return 1;
	}
	else
	{
		to_client_msg.type = 's';
		strcpy(to_client_msg.error,"userinfo change success");
		send(sockfd,&to_client_msg,sizeof(MSG_S),0);
		show_table("staff_table");
		return 0;
	}
}
//添加用户数据
//作者：李超
int admin_add_userinfo_module(INFO x,int sockfd,MSG_S to_client_msg)
{
	if(is_name_exist(x.name))
	{
		strcpy(to_client_msg.error,"name exit");
		to_client_msg.type = 'f';
		send(sockfd,&to_client_msg,sizeof(MSG_S),0);
		return 1;
	}else{
		if(do_staff_add_user(x) == 0)
		{
			to_client_msg.type = 's';
			send(sockfd,&to_client_msg,sizeof(MSG_S),0);
			show_table("staff_table");
			return 0;
		}else{
			strcpy(to_client_msg.error,"insert fail");
			to_client_msg.type = 'f';
			send(sockfd,&to_client_msg,sizeof(MSG_S),0);
			return -1;
		}
	}

}
//删除用户数据
//作者 宋亚武
int admin_del_userinfo_module(char *name,int sockfd,MSG_S to_client_msg)
{


	if(is_name_exist(name)==0)
	{
		to_client_msg.type = 'f';
		strcpy(to_client_msg.error,"name is not exit");
		send(sockfd,&to_client_msg,sizeof(MSG_S),0);
		return -1;
	}
	if(do_staff_del_user(name))
	{
		to_client_msg.type = 'f';
		strcpy(to_client_msg.error,"fail to delete");
		send(sockfd,&to_client_msg,sizeof(MSG_S),0);
		return -1;
	}
	else
	{
		to_client_msg.type = 's';
		send(sockfd,&to_client_msg,sizeof(MSG_S),0);
		return 0;
		
	}
}
//作者: 潘水养
//参数：字符串
//功能：将字符串转换为float型
//适用：字符串
//返回值：返回float型

float str_float(char *s)  
{
    unsigned int i = 0;
	int num_int = 0, count = 1 ,sign = 1;
	float num_decm = 0;

	if(s[0] == '-' )
	{
		sign = -1;
		i ++;
	}
	for(;s[i] != '.'&&s[i] != '\0';i++)
	{
		num_int = num_int * 10 + s[i] - '0'; 

	}
	if(i < strlen(s))
	{
		for(i = i+1 ; s[i] != '\0'; i++)
		{
			count *= 10;

			num_decm = num_decm *10 + s[i] - '0';
		}

		num_decm = num_decm / count;	

		return  sign*(num_int + num_decm) ;
	}

	else return (float)(sign*num_int);

}
//作者：盘水养
//参数：要查询名字，与客户端连接的标识符
//功能：服务器查询员工信息后发送给客户端
//适用：服务器查询数据库信息
//返回值：0 表示发送给客户端成功 1表示发送给客户端失败




int user_search_info_module(char *name,int sockfd)
{
	MSG_S y;
	bzero(&y,sizeof(MSG_S));
	char **arr = do_search_userinfo(name);
	if(arr!=NULL)
	{
		printf("arr  1\n");
		float salary = str_float(arr[12]);
		printf("arr  2\n");
	    y.text.salary = salary;
	    int age1 = atoi(arr[11]);
	    y.text.age = age1;
	    int flag1 = atoi(arr[13]);
	    y.text.flag = flag1;
		y.type = 's';
		strcpy(y.text.id,arr[7]);
		strcpy(y.text.name,arr[8]);
		strcpy(y.text.password,arr[9]);

		strcpy(y.text.addr,arr[10]);
	    sqlite3_free_table(arr);
		printf("arr  3\n");
	}
	else
	{
		y.type = 'f';
		strcpy(y.error,"fail to seach");
		
	}
    
	if(send(sockfd,&y,sizeof(MSG_S),0) > 0)
	{
		printf("send success\n");
		return 0;
	}
	else
	{
		printf("send fail\n");
		return -1;
	}


}



